/* Copyright (c) 2021, National University of Defense Technology. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include "coo_io.h"
#include "csr_io.h"

namespace YHAMG
{

void CSR_Read(const char* filename, CSR_Matrix& A)
{
	COO_Matrix B;
	COO_Read(filename, B);
	A = B;
}

void CSR_Write(const char* filename, const CSR_Matrix& A)
{
	int n = A.size[0];
	int m = A.size[1];
	int nz = A.rowptr[A.size[0]];

	int* Ap = A.rowptr;
	int* Ai = A.colind;
	double* Av = A.values;

	FILE* f = fopen(filename, "w");

	fprintf(f, "%d %d %d", n, m, nz);

	for (int i = 0; i < n; ++i)
		for (int j = Ap[i]; j < Ap[i + 1];++j)
			fprintf(f, "\n%d %d %20.16g", i + 1, Ai[j] + 1, Av[j]);

	fclose(f);
}

}